﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MedicLibrary.Model;
using MedicLibrary.Controler;

namespace MedicLibrary.View
{
    public partial class frmDSHoatChat : Form
    {
        public String action;

        public delegate void getHoatChat(int ID);

        public getHoatChat getHC;
 
        public frmDSHoatChat()
        {
            InitializeComponent();
        }

        private void frmDSNhomThuoc_Load(object sender, EventArgs e)
        {
            dgvDSHoatChat.DataSource = HoatChat.getDanhSachHoatChat("");
        }

        private void dgvDSNhomThuoc_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.action == "LIST")
            {
                Session.IDHoatChat = Int32.Parse(dgvDSHoatChat.SelectedRows[0].Cells["ID"].Value.ToString());
                frmChiTietHoatChat frmCTHC = new frmChiTietHoatChat();
                frmCTHC.applyAction("VIEW");
                frmCTHC.Show();
               
            }
            else if (this.action == "SEARCH")
            {
                getHC(Int32.Parse(dgvDSHoatChat.SelectedRows[0].Cells["ID"].Value.ToString()));
                this.Close();
            }
            
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmChiTietHoatChat ctNhom = new frmChiTietHoatChat();
            ctNhom.applyAction("ADD");
            ctNhom.ShowDialog();
            
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Session.IDNhomThuoc = Int32.Parse(dgvDSHoatChat.SelectedRows[0].Cells["ID"].Value.ToString());
            frmChiTietHoatChat ctNhom = new frmChiTietHoatChat();
            ctNhom.applyAction("EDIT");
            ctNhom.ShowDialog();
           
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            HoatChat hc = new HoatChat();
            hc.ID = Int32.Parse(dgvDSHoatChat.SelectedRows[0].Cells["ID"].Value.ToString());
            hc.deleteHoatChat();

            dgvDSHoatChat.DataSource = HoatChat.getDanhSachHoatChat(txtTimKiem.Text);
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            dgvDSHoatChat.DataSource = HoatChat.getDanhSachHoatChat(txtTimKiem.Text);
        }

        private void btnXemHet_Click(object sender, EventArgs e)
        {
            dgvDSHoatChat.DataSource = HoatChat.getDanhSachHoatChat("");
            txtTimKiem.Text = "";
        }

        public void applyAction(String ac)
        {
            this.action = ac;
            bool status = false;
            if (this.action == "LIST")
            {
                status = true;
            }
            else if (this.action == "SEARCH")
            {
                status = false;
            }
            this.btnThem.Enabled = status;
            this.btnXoa.Enabled = status;
            this.btnSua.Enabled = status;
        }

        private void txtTimKiem_KeyUp(object sender, KeyEventArgs e)
        {
            dgvDSHoatChat.DataSource = HoatChat.getDanhSachHoatChat(txtTimKiem.Text);
        }
    }
}
